package com.bingxue.edu.framework.security;

import cn.dev33.satoken.dao.SaTokenDao;
import cn.dev33.satoken.dao.SaTokenDaoRedissonJackson;
import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.router.SaRouter;
import com.bingxue.edu.management.auth.EmployeeAuthUtil;
import com.bingxue.edu.parent.util.ParentAuthUtil;
import org.redisson.api.RedissonClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * @author panbo
 * @since 2024/6/28
 */
@Configuration
public class SecurityAutoConfiguration implements WebMvcConfigurer {

    private final List<String> excludeUrls = List.of(
            "/favicon.ico",
            "/doc.html",
            "/webjars/**",
            "/v3/api-docs/**"
    );

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new SaInterceptor(handle -> {
            SaRouter.match("/p/**").check(r -> ParentAuthUtil.STP.checkLogin()).stop();
            SaRouter.match("/**").check(r -> EmployeeAuthUtil.STP.checkLogin());
        })).addPathPatterns("/**").excludePathPatterns(excludeUrls);
    }

    @Bean
    public SaTokenDao saTokenDao(RedissonClient redissonClient) {
        return new SaTokenDaoRedissonJackson(redissonClient);
    }
}
